package com.elves.auth.entity.supplier;


import java.util.Arrays;
import java.util.List;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;

/**
 * 供应商表
 */
@Slf4j
@Entity(name = "supplier")
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "supplier")
public class Supplier {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id; //主键

    private String name; //名称
    private String address; //供应商总部所在地址
    private LocalDateTime time;//更新时间
    private String area;//合作产品供应领域
    private String contacts;//联系人
    private String tel;//联系电话
    private String capital;//注册资本
    @Column(name = "establish_time")
    private String establishTime;// 成立时间
    private String evaluation;// 供应商评价

    // 会自动关联到表：supplier_alias
    @ElementCollection
    @CollectionTable(name = "supplier_alias", joinColumns = @JoinColumn(name = "supplier_id"))
    @Column(name = "alias")
    private List<String> alias;

    private String note;// 备注

    public static Supplier mapToSupplier(LinkedHashMap<Integer, Object> map) {
        Supplier supplier = new Supplier();
        supplier.setId(Integer.valueOf(map.get(0).toString()));
        supplier.setName((String) map.get(1));
        supplier.setAddress((String) map.get(2));
        Object time = map.get(3);
        if (time != null) {
            //time 字符串解析为LocalDateTime
            supplier.setTime(LocalDateTime.parse(time.toString()));
        }else{
            supplier.setTime(LocalDateTime.now());
        }
        supplier.setArea((String) map.get(4));
        supplier.setContacts((String) map.get(5));
        supplier.setTel((String) map.get(6));
        supplier.setCapital((String) map.get(7));
        supplier.setEstablishTime((String) map.get(8));
        supplier.setEvaluation((String) map.get(9));
        if (map.get(10) != null) {
            String alias = map.get(10).toString();
            log.info("alias:{}", alias.split(","));
            supplier.setAlias((Arrays.asList(alias.split(","))));
        }
        supplier.setNote((String) map.get(11));
        return supplier;
    }
}
